package org.coast.mvc.logging;


import java.io.*;
import java.util.*;

/**
 * 帮助判断推测最佳logger
 * <p>
 * 读取配置项
 * 1. classpath : coast-log.properties
 * 2. java options : coast-log.properties
 */
public class LoggerHelper {

    //前缀
    private static final String PREFIX = "coast.log";

    private static final String DEFAULT = "default";

    private static Properties properties = new Properties();

    private static volatile LoggerHelper loggerHelper;

    static {
        // 1.取配置项 classPath
        InputStream inputStream = ClassLoader.getSystemClassLoader().getResourceAsStream("coast-log.properties");
        try {
            properties.load(inputStream);
        } catch (IOException e) {
            throw new RuntimeException("can load resource from classpath:/coast-log.properties");
        }
        // 2.获取JAVA OPTION属性值
        String fileLocaltion = System.getProperty("coast-log.properties");
        if (null != fileLocaltion && fileLocaltion.length() > 0) {
            File file = new File(fileLocaltion);
            try {
                FileInputStream fileInputStream = new FileInputStream(file);
                properties.load(fileInputStream);
            } catch (FileNotFoundException e) {
                throw new RuntimeException("file not found in java options [coast-log.properties]:" + fileLocaltion);
            } catch (IOException e) {
                throw new RuntimeException("can load resource in java options [coast-log.properties]:" + fileLocaltion);
            }
        }
    }

    private LoggerHelper() {
        System.out.println("LoggerHelper init");
    }

    public static LoggerHelper getLoggerHelper() {
        if (Objects.isNull(loggerHelper)) {
            synchronized (LoggerHelper.class) {
                if (Objects.isNull(loggerHelper)) {
                    loggerHelper = new LoggerHelper();
                }
            }
        }
        return loggerHelper;
    }

    /**
     * 根据名称推断logger实现
     *
     * @param name
     * @return
     */
    public List<LogHandler> deduceLogHandler(String name) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        String preName = PREFIX + name;
        Set<Object> keys = properties.keySet();
        if (keys.size() > 0) {
            // 默认handler实现
            return getLogHandler(preName);
        }
        return getDefaultHandler();
    }

    /**
     * 获取默认的logHandler
     *
     * @return
     */
    private List<LogHandler> getDefaultHandler() {
        String property = properties.getProperty(PREFIX + DEFAULT + ".handlers");
        //todo 获取默认logHandler
        return null;
    }

    /**
     * 获取LoggerHandler
     *
     * @param preName
     * @return
     * @throws ClassNotFoundException
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    private static List<LogHandler> getLogHandler(String preName) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        String handlerKey = preName + ".handlers";
        String classNames = properties.getProperty(handlerKey);
        String[] classNameArr = classNames.split(",");
        for (String className : classNameArr) {
            if (className != null && className.length() > 0) {
                Class<?> handlerClass = Class.forName(className);
                boolean assignableFrom = handlerClass.isAssignableFrom(LoggerHelper.class);
                if (!assignableFrom) {
                    throw new RuntimeException("");
                }
                return new ArrayList<LogHandler>() {{
                    add((LogHandler) handlerClass.newInstance());
                }};
            }
        }
        return null;
    }


}
